package com.课上案例练习.线程.线程安全.线程死锁;

public class Test1  {
    public static void main(String[] args)throws Exception {
        new Thread(new DeadLock(1),"线程A").start();
        new Thread(new DeadLock(0),"线程B").start();
    }
}
class DeadLock implements Runnable{
    public DeadLock(int flag) {
        this.flag = flag;
    }

    public int flag=1;
   //静态对象是类的所有对象共享
   private static Object o1=new Object(),o2=new Object();

    @Override
    public void run() {
        if(flag==1){
            synchronized (o1){
                System.out.println(Thread.currentThread().getName()+"获取o1锁");
                synchronized (o2){
                    System.out.println(Thread.currentThread().getName()+"获取o2锁");
                }
            }
        }
        if(flag==0) {
          synchronized (o2){
              System.out.println(Thread.currentThread().getName()+"获取o2锁");
               synchronized (o1){
                   System.out.println(Thread.currentThread().getName()+"flag==0获取o1锁");
               }

          }
        }
    }
}